/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 23, 2012  2:23:56 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.construction
 * File Name : PurchaseRequestBudgetServices.java
 * TODO
 */
package jpa.service.promis.construction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import org.apache.log4j.Logger;

import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.construction.PurchaseRequestBudget;
import jpa.entitys.promis.construction.PurchaseRequestBudgetPK;
import jpa.entitys.promis.construction.SyncCostPropunit;
import jpa.service.common.JPAService;
import promis.utility.CommonService;

/**
 * @author chatree
 *
 */
public class PurchaseRequestBudgetServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(PurchaseRequestBudgetServices.class);

	public PurchaseRequestBudgetServices(EntityManager em) {
		super(em);
	}

	/**
	 * 
	 * Date =Nov 14, 2013 : 10:25:34 AM 
	 * CreateBy=Chatree Siangklom
	 * PurchaseRequestBudget
	 * TODO
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public PurchaseRequestBudget findById(PurchaseRequestBudgetPK id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "PurchaseRequestBudget.findById"));				
	}

	/**
	 * 
	 * Date =Nov 14, 2013 : 11:03:26 AM 
	 * CreateBy=Chatree Siangklom
	 * List<PurchaseRequestBudget>
	 * TODO
	 * @param company
	 * @param project
	 * @param porType
	 * @param porSeries
	 * @param porNo
	 * @param seqNo
	 * @return
	 * @throws Exception
	 */
	public PurchaseRequestBudget findBy6Key(PurchaseRequestBudgetPK id) throws Exception {
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", id.getCompany());
		mapWhereValue.put("project", id.getProject());
		mapWhereValue.put("porType", id.getPorType());
		mapWhereValue.put("porSeries", id.getPorSeries());
		mapWhereValue.put("porNo", id.getPorNo());
		mapWhereValue.put("seqNo", id.getSeqNo());
		
		List<PurchaseRequestBudget> lst = super.ConvertToYoureObject(super.find("PurchaseRequestBudget.findBy6Key", mapWhereValue));
		if(CommonService.getSize(lst) == 1){
			return lst.get(0);
		}else
			throw new Exception("Error: Result more than one records [Size=" + CommonService.getSize(lst) + "]");
		
	}
	
	/**
	 * 
	 * Date =Nov 14, 2013 : 10:25:40 AM 
	 * CreateBy=Chatree Siangklom
	 * PurchaseRequestBudget
	 * TODO
	 * @param vlaue
	 * @return
	 * @throws Exception
	 */
	public PurchaseRequestBudget InsertedOrUpdated(PurchaseRequestBudget vlaue) throws Exception {
		PurchaseRequestBudget obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdOn()))
			{		
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.inserted(obj);
			}else{
				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
		}catch(Exception e)
		{
			log.error(e.getMessage());
			throw e;
		}
		return obj;
	}
	
	/**
	 * 
	 * Date =Nov 14, 2013 : 10:25:43 AM 
	 * CreateBy=Chatree Siangklom
	 * List<PurchaseRequestBudget>
	 * TODO
	 * @param vlauelist
	 * @return
	 * @throws Exception
	 */
	public List<PurchaseRequestBudget> Inserted(List<PurchaseRequestBudget> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(PurchaseRequestBudget obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{			
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}		
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}
	
	/**
	 * 
	 * Date =Nov 14, 2013 : 10:25:46 AM 
	 * CreateBy=Chatree Siangklom
	 * List<PurchaseRequestBudget>
	 * TODO
	 * @param vlauelist
	 * @return
	 * @throws Exception
	 */
	public List<PurchaseRequestBudget> Updated(List<PurchaseRequestBudget> vlauelist) throws Exception {		
		
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			for(PurchaseRequestBudget obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);					
				}
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);			
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}
	
}
